//
//  SRDeviceInfo.m
//  sr01sdkProject
//
//  Created by  on 2022/5/25.
//

#import "CADeviceInfo.h"
#import "OTAHeader.h"

@implementation CADeviceInfo

- (instancetype)initWithData:(NSData *)data
{
    self = [super init];
    if (self) {
        
        if (data.length < 20) {
            return  self;
        }
        uint8_t *dataBytes = (uint8_t *)[data bytes];
        
        uint8_t color = dataBytes[2];
        _color = color;
        
        uint8_t size = dataBytes[3];
        _size =  size;
        
        NSString *macAddress = [NSString stringWithFormat:@"%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
                                dataBytes[9], dataBytes[8],
                                dataBytes[7],dataBytes[6],
                                dataBytes[5],dataBytes[4]];
        _bleMacAddress = macAddress;
        
        NSString *version = [NSString stringWithFormat:@"%x.%x.%x", dataBytes[10], dataBytes[11], dataBytes[12]];
        _softWareVersion = version;
        _needOemAuth = dataBytes[13] > 0; // 是否需要厂家认证
        // 充电模式
        _chargeType = dataBytes[14];
        // 主芯片型号
        _mainChip = dataBytes[15];
        //
        _generation = dataBytes[16];

        // functions
        uint8_t devFunctions = dataBytes[17];
        
        uint8_t supportExercise = devFunctions & 0x01; //是否支持运动模式
        uint8_t supportEcg = (devFunctions >> 1) & 0x01;
        
        uint8_t ecgCalcType = (devFunctions >> 2) & 0x01;//ecg结果: 0 为设备计算，1 为云端计算
        
        _ecgCalcType = ecgCalcType==0 ? CA_ECG_CALC_ON_DEVICE : CA_ECG_CALC_ON_NET;
        
        _isSelfDevelopedAlgorithm = (((devFunctions >> 3) & 0x01) == 0);// 0 为凌拓算法，1 为 NALG 算法
        
        _supportSportmode = supportExercise > 0;
        _supportEcg = supportEcg > 0;
        
        _projectId = dataBytes[18] & 0XFF;
        DebugNSLog(@"_projectId = 0X%X", _projectId);
        
    }
    return self;
}



-(NSString *)chooseOTAugument
{
    NSString *cat = nil;
  
    if (self.projectId == CA_PROJ_SR28_NFC) {
        // sr23
        cat = SR28_CAT;
    }
    
    if (self.projectId == CA_PROJ_SR28C) {
        cat = nil; // 25-3-31 暂不支持
    }
    
    if (self.projectId == CA_PROJ_SR28D) {
        // sr23D
        cat = SR23D_CAT;
    }
    
    // 转换为base64+urlenc编码
    NSString *encodeCat = [self transCatToBs64UrlEncode:cat];
    
    return encodeCat;
   
}

-(NSString *)transCatToBs64UrlEncode:(NSString *)cat {
    
    if (!cat.length) {
        return nil;
    }
    
//    NSDictionary *dict = @{
//        @"sr09_t" : @"c3IwOV90",
//        @"nexring03_t" : @"bmV4cmluZzAzX3Q%3D",
//        @"sr26_t" : @"c3IyNl90",
//        @"sr09n_t" : @"c3IwOW5fdA%3D%3D",
//        @"sr23_t" : @"c3IyM190",
//
//        @"nexring03" : @"bmV4cmluZzAz",
//        @"sr26" : @"c3IyNg%3D%3D",
//        @"sr23" : @"c3IyMw%3D%3D",
//        @"sr09" : @"c3IwOQ%3D%3D",
//        @"sr09n" : @"c3IwOW4%3D",
//
//    };
    
    NSString *base64Encode =  [self base64UrlEncode:cat];
    
    return base64Encode;//dict[cat];
    
    
}


-(NSString *)base64UrlEncode:(NSString *)inputString {
    if (!inputString) {
        return nil;
    }
//    NSString *inputString = @"Your input string";
    NSData *data = [inputString dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64EncodedString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    // urlencode
    
    NSCharacterSet *allowedCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]"] invertedSet];

    NSString *encodedUrlString = [base64EncodedString stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];

    return  encodedUrlString;
}


-(BOOL)checkNeedUpdate:(NSString *)remoteVersion
{
    if (!remoteVersion) {
        return NO;
    }
    
    NSString *localVer = self.softWareVersion;
    
    if ([self versionIsLowThan:remoteVersion LocalVer:localVer]) {
        
        return YES;
    }
    return NO;
}


-(BOOL)versionIsLowThan:(NSString *)remote LocalVer:(NSString *)local {
    
    NSArray<NSString *> *arrLocal =  [local componentsSeparatedByString:@"."];
    NSArray<NSString *> *arrRemote =  [remote componentsSeparatedByString:@"."];
    if (arrLocal.count != arrRemote.count) {
        return NO; //格式不匹配
    }
    BOOL isLow = NO;
    for (int i = 0; i < arrLocal.count; i++) {
        if ([arrLocal[i] intValue] < [arrRemote[i] intValue]) {
            isLow = YES;
            break;
        }
        
    }
    
    return isLow;
}


@end
